package com.veo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.veo.entity.Admin;
import com.veo.entity.User;
import com.veo.service.AdminService;
import com.veo.service.UserService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;


@Component("loginAction")
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/")
@Results({
        @Result(name = "login",location = "/show.jsp",type = "redirect"),
        @Result(name = "codeError",location = "/login.jsp"),
        @Result(name = "readerPage",location = "/reader/readerMain.jsp",type = "redirect"),
        @Result(name = "adminPage",location = "/main.jsp"),
})
public class LoginAction extends ActionSupport implements ModelDriven<User> {

    private User user=new User();
    @Override
    public User getModel() {
        return user;
    }

    @Autowired
    private UserService userService;

    @Autowired
    private AdminService adminService;

    @Action("login")
    public String login(){
        String checkCode = (String) ServletActionContext.getRequest().getSession().getAttribute("checkCode");

        if (!checkCode.equalsIgnoreCase(user.getCheckCode())){
            ServletActionContext.getRequest().setAttribute("msg","验证码错误！");
            return "codeError";
        }
        if ("0".equals(user.getType())){
            User findUser =userService.findLoginUser(user);
            if (findUser!=null){
                if (user.getRememberMe()!=null){
                    //记住用户，将用户密码和用户名存入cookie
                    Cookie rememberUser=new Cookie("rememberUser", user.getUsername()+"&"+user.getPassword()+"&"+user.getType()+"&"+"true");
                    rememberUser.setMaxAge(60*60*24);
                    ServletActionContext.getResponse().addCookie(rememberUser);
                    System.out.println("记住用户登录！");
                }
                ServletActionContext.getRequest().getSession().setAttribute("loginUser",findUser);
                HttpServletRequest request = ServletActionContext.getRequest();
                Cookie[] cookies = request.getCookies();
                try {
                    for (Cookie cookie : cookies) {
                        if ("rememberUser".equals(cookie.getName())) {
                            Cookie cookieNew = new Cookie(cookie.getName(), null);
                            cookieNew.setMaxAge(0);
                            ServletActionContext.getResponse().addCookie(cookieNew);
                        }
                    }
                }catch(Exception ex) {
                    System.out.println("清空Cookies发生异常！");
                }
            }else {
                ServletActionContext.getRequest().setAttribute("msg","用户名或密码错误！");
                return "codeError";
            }
            return "readerPage";
        }else {
            Admin admin=new Admin();
            admin.setUsername(user.getUsername());
            admin.setPassword(user.getPassword());
            Admin findUser=adminService.findLoginAdmin(admin);
            if (findUser!=null){
                if (user.getRememberMe()!=null){
                    //记住用户，将用户密码和用户名存入cookie
                    Cookie rememberUser=new Cookie("rememberUser", user.getUsername()+"&"+user.getPassword()+"&"+user.getType()+"&"+"true");
                    rememberUser.setMaxAge(60*60*24);
                    ServletActionContext.getResponse().addCookie(rememberUser);
                    System.out.println("记住用户登录！");
                }
                ServletActionContext.getRequest().getSession().setAttribute("loginUser",findUser);
                HttpServletRequest request = ServletActionContext.getRequest();
                Cookie[] cookies = request.getCookies();
                try {
                    for (Cookie cookie : cookies) {
                        if ("rememberUser".equals(cookie.getName())) {
                            Cookie cookieNew = new Cookie(cookie.getName(), null);
                            cookieNew.setMaxAge(0);
                            ServletActionContext.getResponse().addCookie(cookieNew);
                        }
                    }
                }catch(Exception ex) {
                    System.out.println("清空Cookies发生异常！");
                }
            }else {
                ServletActionContext.getRequest().setAttribute("msg","用户名或密码错误！");
                return "codeError";
            }
            return "adminPage";
        }




       /* User findUser =userService.findLoginUser(user);
        if (findUser!=null){
            if (user.getRememberMe()!=null){
                //记住用户，将用户密码和用户名存入cookie
                Cookie rememberUser=new Cookie("rememberUser", user.getUsername()+"&"+user.getPassword()+"&"+user.getType()+"&"+"true");
                rememberUser.setMaxAge(60*60*24);
                ServletActionContext.getResponse().addCookie(rememberUser);
                System.out.println("记住用户登录！");
            }
            ServletActionContext.getRequest().getSession().setAttribute("loginUser",findUser);
            HttpServletRequest request = ServletActionContext.getRequest();
            Cookie[] cookies = request.getCookies();
            try {
                for (Cookie cookie : cookies) {
                    if ("rememberUser".equals(cookie.getName())) {
                        Cookie cookieNew = new Cookie(cookie.getName(), null);
                        cookieNew.setMaxAge(0);
                        ServletActionContext.getResponse().addCookie(cookieNew);
                    }
                }
            }catch(Exception ex) {
                System.out.println("清空Cookies发生异常！");
            }
            if ("0".equals(user.getType())){
                return "readerPage";
            }else {
                return "adminPage";
            }
        }else {
            ServletActionContext.getRequest().setAttribute("msg","用户名或密码错误！");
            return "codeError";
        }*/
    }


    @Action("loginOut")
    public String loginOut(){
        ServletActionContext.getRequest().getSession().invalidate();
        return "login";
    }
}
